package com.etc.controller;


import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.Message;
import com.etc.util.UserLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户登录控制器
 */
@RestController
public class LoginController {
    //注入用户登录工具类
    @Autowired
    private UserLoginUtil userLoginUtil;

    @Autowired
    private UserService userService;

    /**
     * @param usertel 获取验证码的手机号
     * @return message 回执消息 验证码 发送状态
     */
    @PostMapping("code")
    public Message getCode(String usertel) {
        Message message = new Message();
        if (usertel != null) {
            User user = userService.findUserByTel(usertel);
            if (user != null) {
                message.setMsg("该手机已经注册过了，请直接登录");
            }
        }
        message = userLoginUtil.getSsm(usertel);
        return message;
    }

    /**
     * @param request HttpServletRequest
     * @param usertel 用户输入的手机号码
     * @param code    随机生成的6位验证码
     * @param yzm     用户输入的验证码
     * @return Message 回执消息
     */
    @PostMapping("login")
    public Message userLogin(HttpServletRequest request, String usertel, Integer code, Integer yzm, HttpServletResponse response) {
        Message message = new Message();
        //判断用户输入的验证码和随机生成的验证码是否一致
        if (yzm.equals(code)) {
            User user = userService.findUserByTel(usertel);
            //判断查询结果是否为空
            if (user != null) {
                HttpSession session = request.getSession();
                message.setUserstatus(user.getUserstatus());
                //把用戶id存到cookie
                String useridForCookie = user.getUserid().toString();
                Cookie userid_cookie = new Cookie("userid", useridForCookie.trim());
                userid_cookie.setMaxAge(30 * 60);// 设置为30min
                userid_cookie.setPath("/");
                response.addCookie(userid_cookie);
                //把用户存到session
                session.setAttribute("user", user);
                message.setMsg("登陆成功");
            } else {
                message.setMsg("请先注册");
            }
        } else {
            message.setMsg("验证码错误");
        }
        return message;
    }

    @PostMapping("register")
    public Message userRegister(User user) {
        Message message = new Message();
        boolean flag = true;
        if (user != null) {
            User u = userService.findUserByTel(user.getUsertel());
            if (u != null) {
                message.setMsg("手机号已注册，请登录");
            } else {
                flag = userService.addUser(user);
                String str = flag ? "注册成功" : "注册失败";
                message.setMsg(str);
            }
        }
        return message;
    }

    @PostMapping("verify")
    public Message userVerify(String username) {
        // System.out.println("asfasfasfsafsafsafsadf"+username);
        Message message = new Message();
        User u1 = userService.findUserByUsername(username);
        if (u1 != null) {
            message.setMsg("用户名已存在");
        } else {
            message.setMsg("用户名不存在");
        }
        return message;
    }

    /**
     * @param request      HttpServletRequest
     * @param username     用户名
     * @param userpassword 密码
     * @return Message 回执消息
     */
    @PostMapping("namelogin")
    public Message nameLogin(HttpServletRequest request, String username, String userpassword, HttpServletResponse response) {
        Message message = userService.findByUsernameAndUserpassword(username, userpassword);
        if (message.getMsg().equals("登录成功")) {
            User user = userService.findUserByUsername(username);
            HttpSession session = request.getSession();
            message.setUserstatus(user.getUserstatus());
            //把用戶id存到cookie
            String useridForCookie = user.getUserid().toString();
            Cookie userid_cookie = new Cookie("userid", useridForCookie.trim());
            userid_cookie.setMaxAge(30 * 60);// 设置为30min
            userid_cookie.setPath("/");
            response.addCookie(userid_cookie);
            session.setAttribute("user", user);
            message.setMsg("登陆成功");
        } else if (message.getMsg().equals("请注册")) {
            message.setMsg("请先注册");
        } else {
            message.setMsg("密码不正确");
        }
        return message;
    }


}
